/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#pragma once
	
/*-----------------------------------------------------------------------------
	QUATERNION.H
-----------------------------------------------------------------------------*/

class EQuaternion {
	public:
						EQuaternion		( void );
						EQuaternion		( float xx, float yy, float zz, float ww );
		explicit		EQuaternion		( const float *floatptr );

		float			*Ptr			( void )		{ return &x; }
		const	float	*Ptr			( void ) const	{ return &x; }

		EQuaternion&	operator +=		( const EQuaternion &q );
		EQuaternion&	operator -=		( const EQuaternion &q );
		EQuaternion&	operator *=		( const EQuaternion &q );
		EQuaternion&	operator /=		( const EQuaternion &q );
		EQuaternion&	operator *=		( float a);
		EQuaternion&	operator /=		( float a);

		EQuaternion		operator +		( void ) const;
		EQuaternion		operator -		( void ) const;

		EQuaternion		operator + 		( const EQuaternion &q ) const;
		EQuaternion		operator - 		( const EQuaternion &q ) const;
		EQuaternion		operator * 		( const EQuaternion &q ) const;
		EQuaternion		operator / 		( const EQuaternion &q ) const;
		EQuaternion		operator * 		( float a ) const;
		EQuaternion		operator / 		( float a ) const;

		bool			operator ==		( const EQuaternion &q ) const;
		bool			operator !=		( const EQuaternion &q ) const;
		
		bool			IsEqual			( const EQuaternion &other, float eps ) const;

		EQuaternion		&SetIdentity		( void );
		
		EQuaternion		Conjugate		( void ) const;
		float			Dot				( const EQuaternion &q ) const;
		EQuaternion		Inverse			( void ) const;
		float			Length			( void ) const;
		float			LengthSqr		( void ) const;

		EQuaternion		Normalize		( void ) const;
		EQuaternion		SLerp			( const EQuaternion &target, float factor ) const;
		
		EMatrix			ToMatrix		( void ) const;
		void			ToAngles		( float &yaw, float &pitch, float &roll ) const;
		void			ToAnglesRad		( float &yaw, float &pitch, float &roll ) const;

	public:
		static const	EQuaternion		kIdentity;
		
	public:
		static EQuaternion	FromMatrix			( const EMatrix &xform );
		static EQuaternion	FromAngles			( float yaw, float pitch, float roll );
		static EQuaternion	FromAnglesRad		( float yaw, float pitch, float roll );
		static EQuaternion	FromString			( const char *str );
		
  		static EQuaternion	RotateAroundAxis	( float angle, const EVector &axis );

	public:
		union {
			float v[4];
			struct { float x, y, z, w; };
		};
	};

/*-----------------------------------------------------------------------------
	QUATERNION IMPLEMENTATION :
-----------------------------------------------------------------------------*/

inline EQuaternion::EQuaternion( void )
{
	SetIdentity();
}


inline EQuaternion::EQuaternion( float xx, float yy, float zz, float ww ) : x(xx), y(yy), z(zz), w(ww)
{

}


inline EQuaternion::EQuaternion( const float *floatptr ) : x(floatptr[0]), y(floatptr[1]), z(floatptr[2]), w(floatptr[3])
{

}


inline EQuaternion& EQuaternion::operator+=( const EQuaternion &q )
{
	x += q.x;
	y += q.y;
	z += q.z;
	w += q.w;
	return *this;
}


inline EQuaternion& EQuaternion::operator-=( const EQuaternion &q )
{
	x -= q.x;
	y -= q.y;
	z -= q.z;
	w -= q.w;
	return *this;
}


inline EQuaternion& EQuaternion::operator*=( const EQuaternion &q )
{
	*this = *this * q;
	return *this;
}


inline EQuaternion& EQuaternion::operator*=( float a)
{
	x *= a;
	y *= a;
	z *= a;
	w *= a;
	return *this;
}


inline EQuaternion& EQuaternion::operator/=( const EQuaternion &q )
{
	*this = *this * q.Inverse();
	return *this;
}


inline EQuaternion& EQuaternion::operator/=( float a)
{
	x /= a;
	y /= a;
	z /= a;
	w /= a;
	return *this;
}


inline EQuaternion EQuaternion::operator+( void ) const
{
	return EQuaternion(x,y,z,w);
}


inline EQuaternion EQuaternion::operator+( const EQuaternion &q ) const
{
	EQuaternion q1 = *this;
	return q1 += q;
}


inline EQuaternion EQuaternion::operator-( void ) const
{
	return EQuaternion(-x,-y,-z,-w);
}


inline EQuaternion EQuaternion::operator-( const EQuaternion &q ) const
{
	EQuaternion q1 = *this;
	return q1 -= q;
}


inline EQuaternion EQuaternion::operator*( const EQuaternion &q ) const
{
	return EQuaternion(	w*q.x + x*q.w + y*q.z - z*q.y,
						w*q.y + y*q.w + z*q.x - x*q.z,
						w*q.z + z*q.w + x*q.y - y*q.x,
						w*q.w - x*q.x - y*q.y - z*q.z );
}


inline EQuaternion EQuaternion::operator*( float a ) const
{
	EQuaternion q1 = *this;
	return q1 *= a;
}


inline EQuaternion EQuaternion::operator/( const EQuaternion &q ) const
{
	return *this * q.Inverse();
}


inline EQuaternion EQuaternion::operator/( float a ) const
{
	return (*this) * (1.0f/a);
}


inline bool EQuaternion::operator==( const EQuaternion &q ) const
{
	return (x==q.x && y==q.y && z==q.z && w==q.w ); 
}


inline bool EQuaternion::operator!=( const EQuaternion &q ) const
{
	return !(*this==q);
}
